package com.moyou.weichat.chat;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.exceptions.HyphenateException;
import com.hyphenate.util.EMLog;
import com.moyou.weichat.bean.ChatTypeBean;
import com.moyou.weichat.utils.ChatUtils;
import com.moyou.weichat.utils.ConstantUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * sj ：2017年10月16日11:03:11
 * name: ts
 * 用途:环信聊天的工具类
 */
public class ChatManger {

    private static ChatManger chatManger;
    private String TAG = "ChatManger";


    //申明一个单例的管理器对象
    public static ChatManger getChatManger() {
        return chatManger == null ? chatManger = new ChatManger() : chatManger;
    }

    public void loadChatDate() {
        //添加环信数据的群组和用户数据
        EMClient.getInstance().groupManager().loadAllGroups();
        EMClient.getInstance().chatManager().loadAllConversations();
    }


    //环信的注册方法
    public void registerHx(String username, String pwd) throws HyphenateException {
        //注册失败会抛出HyphenateException
        EMClient.getInstance().createAccount(username, pwd);//同步方法
    }

    //获取搜有的会话
    public List<EMConversation> getChatConversationList(Context mContext) {
        Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
        List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
        /**
         * lastMsgTime will change if there is new message during sorting
         * so use synchronized to make sure timestamp of last message won't change.
         */
        synchronized (conversations) {
            for (EMConversation conversation : conversations.values()) {
                if (conversation.getAllMessages().size() != 0) {
                    sortList.add(new Pair<>(conversation.getLastMessage().getMsgTime(), conversation));
                }
            }
        }
        try {
            // Internal is TimSort algorithm, has bug
            ChatUtils.getChatUtils(mContext).sortConversationByLastChatTime(sortList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<EMConversation> list = new ArrayList<EMConversation>();
        for (Pair<Long, EMConversation> sortItem : sortList) {
            list.add(sortItem.second);
        }
        return list ;
    }


    //获取搜所有会话
    public EMConversation getChatMessageConversation(String toChatUsername){
       return EMClient.getInstance().chatManager().getConversation(toChatUsername, EMConversation.EMConversationType.Chat, true);
    }


//        //SDK初始化加载的聊天记录为20条，到顶时需要去DB里获取更多
//        //获取startMsgId之前的pagesize条消息，此方法获取的messages SDK会自动存入到此会话中，APP中无需再次把获取到的messages添加到会话中
//        List<EMMessage> messages = conversation.loadMoreMsgFromDB(startMsgId, 20); //第一次默认加载20条之后器数据库自己设置数据拿去数据

    /**
     * get unread message count
     * 获取获取未读消息总数
     * @return
     */
    public int getUnreadMsgCountTotal() {
        return EMClient.getInstance().chatManager().getUnreadMsgsCount();
    }
}
